const util = require('../utils/js/util')
const env = wx.getAccountInfoSync().miniProgram.envVersion
const domain = ['develop', 'trial'].includes(env) ? 'https://szb.demo-dev.cn' : env == 'release' ? 'https://mp.zhisuwork.com' : ''

/**
 * 请求方法
 * @param {string} url 请求地址
 * @param {object} data 请求参数
 * @returns {Promise<object>} 返回结果
 */
 const request = (url, method, data = {}) => new Promise((resolve, reject) => {
    const err_reject = e => util.toast(e || '网络错误，请联系管理员')
    wx.request({
        data,
        url: `${domain}${url}`,
        method,
        header: { 'token': getApp().globalData?.userInfo?.token || '' },
        success: res => {
            if (res.statusCode === 200 && res.errMsg === "request:ok") {
                res.data.code === 200 || res.data.code === 1 ? resolve(res.data.data) : (reject(res.data), data?.nopop || util.toast(res.data.msg))
            } else {
                reject(res)
                data?.nopop || err_reject()
            }
        },
        fail: res => (reject(res), err_reject())
    })
})
// 登录
exports.login = data => request('/api/user/wxAppLogin', 'POST', data)
// 获取banner
exports.getBanner = data => request('/api/system/getBannerList', 'GET', data)
// 获取用户二维码
exports.getUserQRCode = (data = {}) => new Promise((resolve, reject) => {
    wx.request({
        data, url: `${domain}/api/system/getUserQRCode`, header: { 'token': getApp().globalData?.userInfo?.token || '' },
        success: res => res.statusCode === 200 && res.errMsg === "request:ok" ? resolve(res.data) : (reject(res), data?.nopop || util.toast('网络错误，请联系管理员')),
        fail: res => (reject(res), util.toast('网络错误，请联系管理员'))
    })
})
// 获取大喇叭列表
exports.getTipsList = data => request('/api/system/getTipsList', 'GET', data)
// 排行榜
exports.getTopList = data => request('/api/system/getTopList', 'GET', data)
// 更新简历信息
exports.profile = data => request('/api/user/profile', 'POST', data)
// 获取用户手机号
exports.getMobile = data => request('/api/user/getMobile', 'POST', data)
// 获取简历配置项
exports.getConfigList = data => request('/api/resume/getConfigList', 'GET', data)
// 获取咨询详情
exports.getDetail = data => request('/api/news/getDetail', 'GET', data)
// 获取邀请列表
exports.getInviteList = data => request('/api/system/getInviteList', 'GET', data)
// 获取二维码配置
exports.getCodeConfig = data => request('/api/system/getConfigList', 'GET', data)
// 获取大喇叭详情
exports.getTips = data => request('/api/system/getTips', 'GET', data)
// 代理商获取指定用户简历信息
exports.getWorkerInfo = data => request('/api/resume/getWorkerInfo', 'GET', data)





